package com.pingba.serviceImpl.msg;

import com.pingba.dao.BaseDao;
import com.pingba.dao.msg.MsgApplyDao;
import com.pingba.dao.msg.MsgSendDao;
import com.pingba.dao.msg.MsgSubscribeDao;
import com.pingba.entity.User;
import com.pingba.entity.msg.MsgApply;
import com.pingba.entity.msg.MsgSend;
import com.pingba.facade.enums.Gender;
import com.pingba.service.msg.MsgSendService;
import com.pingba.serviceImpl.BaseServiceImpl;
import com.pingba.util.helper.JsonHelp;
import com.pingba.util.helper.StrHelper;
import com.pingba.view.apply.MsgSendWithApplyInfoView;
import com.pingba.view.nearby.DbQueryView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * Created by Administrator on 2015/11/2 0002.
 */
@Service("MsgSendService")
public class MsgSendServiceImpl extends BaseServiceImpl<MsgSend, Long> implements MsgSendService {
    @Autowired
    private MsgSendDao msgSendDao;

    @Autowired
    private MsgApplyDao msgApplyDao;

    @Autowired
    private MsgSubscribeDao msgSubscribeDao;

    private static Logger logger = Logger.getLogger(MsgSendServiceImpl.class);
    @Override
    public BaseDao<MsgSend, Long> getDao() {
        return msgSendDao;
    }

    @Override
    public void setReplied(MsgSend msgSend) {
        msgSendDao.setReplied(msgSend);
    }

    @Override
    public void setReaded(MsgSend msgSend) {
        msgSendDao.setReaded(msgSend);
    }

    @Override
    public List<MsgSend> getUnhandled(long userId) {
        return msgSendDao.getUnhandled(userId);
    }

    @Override
    public List<MsgSend> getMysends(long userId) {
        return msgSendDao.getMysends(userId);
    }



    @Override
    public List<MsgSend> nearByList(float longitude, float latitude, String query, int curPage, int orderType, int gender, long userId) {
        int range = 500 * 1000;
        double r = 180 / Math.PI * range / 6372797;      //
        double lngR = r / Math.cos(latitude * Math.PI / 180.0);
        double maxLat = latitude + r;
        double minLat = latitude - r;
        double maxLng = longitude + lngR;
        double minLng = longitude - lngR;
        long now = new Date().getTime();
        DbQueryView dbQueryView = new DbQueryView();
        dbQueryView.setGender(gender);
        dbQueryView.setMaxLatitude(maxLat);
        dbQueryView.setMinLatitude(minLat);
        dbQueryView.setMaxLongitude(maxLng);
        dbQueryView.setMinLongitude(minLng);
        dbQueryView.setNow(now);
        dbQueryView.setQuery(StrHelper.isNullOrEmpty(query) || query.equals("null") ? null : query);
        dbQueryView.setUserId(userId);
        List<MsgSend> tmpList = msgSendDao.near(dbQueryView);
        System.out.println(JsonHelp.jsonFromObject(tmpList));
        return tmpList;

    }





    /**
     * 这个数学公式可以使用，但不是非常的精确
     *
     * @param longitude
     * @param latitude
     * @param range     单位米
     * @return
     */
    public List<MsgSend> nearByMap(double longitude, double latitude, int range, int gender, long userId) {
        double r = 180 / Math.PI * range / 6372797;      //
        double lngR = r / Math.cos(latitude * Math.PI / 180.0);
        double maxLat = latitude + r;
        double minLat = latitude - r;
        double maxLng = longitude + lngR;
        double minLng = longitude - lngR;
        long now = new Date().getTime();
        DbQueryView dbQueryView = new DbQueryView();
        dbQueryView.setGender(gender);
        dbQueryView.setMaxLatitude(maxLat);
        dbQueryView.setMinLatitude(minLat);
        dbQueryView.setMaxLongitude(maxLng);
        dbQueryView.setMinLongitude(minLng);
        dbQueryView.setNow(now);
        dbQueryView.setQuery(null);
        dbQueryView.setUserId(userId);

        List<MsgSend> tmpList = msgSendDao.near(dbQueryView);
        logger.info("map result is: " + JsonHelp.jsonFromObject(tmpList));

        return tmpList;

    }

    @Override
    public Map<Long, Map<String, Object>> myMsgWithApply(User me) {
        List<MsgSendWithApplyInfoView> viewList = msgSendDao.myMsgWithApply(me);
        System.out.println("size is: " + viewList.size());
        String msgSendkey = "msgSend";
        String msgApplyKey = "msgApply";
        Map<Long, Map<String, Object>> result = new HashMap<Long, Map<String, Object>>();
        for (MsgSendWithApplyInfoView view : viewList) {
            System.out.println(view);
            Map<String, Object> tmp = result.get(view.getSid());
            if (tmp == null) {
                tmp = new HashMap<String, Object>();
                MsgSend send = new MsgSend();
                send.setSid(view.getSid());
                send.setLongitude(view.getLongitude());
                send.setLatitude(view.getLatitude());
                send.setLocation(view.getLocation());
                send.setContent(view.getContent());
                send.setTitle(view.getTitle());
                send.setImgUrl1(view.getImgUrl1());
                send.setImgUrl2(view.getImgUrl2());
                send.setPublishTime(view.getPublishTime());
                tmp.put(msgSendkey, send);
                tmp.put(msgApplyKey, new ArrayList<MsgApply>());
                result.put(view.getSid(), tmp);
            }
            MsgApply apply = new MsgApply();
            apply.setSid(view.getAid());
            apply.setApplicantId(view.getApplicantId());
            apply.setApplicantName(view.getApplicantName());
            apply.setApplicantHeadUrl(view.getApplicantHeadUrl());
            apply.setApplicantContent(view.getApplicantContent());
            apply.setStatus(view.getStatus());
            apply.setApplyTime(view.getApplyTime());
            List<MsgApply> applyList = (List<MsgApply>) tmp.get(msgApplyKey);
            applyList.add(apply);
        }
        return result;
    }

    @Override
    public Map<Long, Map<String, Object>> newMyMsgWithApply(User me) {
        List<MsgSendWithApplyInfoView> viewList = msgSendDao.newMyMsgWithApply(me);

        String msgSendkey = "msgSend";
        String msgApplyKey = "msgApply";
        Map<Long, Map<String, Object>> result = new HashMap<Long, Map<String, Object>>();
        for (MsgSendWithApplyInfoView view : viewList) {
            System.out.println(view);
            Map<String, Object> tmp = result.get(view.getSid());
            if (tmp == null) {
                tmp = new HashMap<String, Object>();
                MsgSend send = new MsgSend();
                send.setSid(view.getSid());
                send.setLongitude(view.getLongitude());
                send.setLatitude(view.getLatitude());
                send.setLocation(view.getLocation());
                send.setContent(view.getContent());
                send.setTitle(view.getTitle());
                send.setImgUrl1(view.getImgUrl1());
                send.setImgUrl2(view.getImgUrl2());
                send.setPublishTime(view.getPublishTime());
                tmp.put(msgSendkey, send);
                tmp.put(msgApplyKey, new ArrayList<MsgApply>());
                result.put(view.getSid(), tmp);
            }
            MsgApply apply = new MsgApply();
            apply.setSid(view.getAid());
            apply.setApplicantId(view.getApplicantId());
            apply.setApplicantName(view.getApplicantName());
            apply.setApplicantHeadUrl(view.getApplicantHeadUrl());
            apply.setApplicantContent(view.getApplicantContent());
            apply.setStatus(view.getStatus());
            apply.setApplyTime(view.getApplyTime());
            List<MsgApply> applyList = (List<MsgApply>) tmp.get(msgApplyKey);
            applyList.add(apply);
        }
        return result;
    }

    @Override
    public List<MsgSend> recentSend(long userId) {
        return msgSendDao.recentSend(userId);
    }

    @Override
    public void deleteMsgSend(long msgSendId, long userId) {
        msgApplyDao.deleteMsgSendApplies(msgSendId, userId);
        msgSendDao.deleteMsgSendAndApplies(msgSendId, userId);
        msgSubscribeDao.deleteSubscribeOfSend(msgSendId);
    }


}
